from django.shortcuts import render

# Create your views here.
from rest_framework import mixins
from rest_framework import status
from rest_framework.generics import CreateAPIView,GenericAPIView
from rest_framework.response import Response
from rest_framework.views import APIView

from users import serializers
from users.models import User




class CheckUsernameView(APIView):

    def get(self,request,username):

        #查询是否有一样的用户名
        count = User.objects.filter(username = username).count()

        data ={
            "count":count
        }

        #返回响应
        return Response(data = data,status=status.HTTP_200_OK)

        pass

    pass

class CheckMobileView(APIView):

    def get(self,request,mobile):

        #查询是否有一样的用户名
        count = User.objects.filter(mobile = mobile).count()

        data ={
            "count":count
        }

        #返回响应
        return Response(data = data,status=status.HTTP_200_OK)

        pass

    pass


class RegisterUserView(CreateAPIView):
    serializer_class = serializers.RegisterUserSerializer

    # def post(self,request):
    # 一, 只继承自了GenericAPIView
    # #1,获取参数
    # dict_data = request.data
    #
    # #2,创建序列化器,校验
    # serializer =  self.get_serializer(data=dict_data)
    # serializer.is_valid(raise_exception=True)
    #
    # #3,数据入库
    # serializer.save()
    #
    # #4,返回响应
    # return Response(serializer.data,status=status.HTTP_201_CREATED)

    # 二,使用了mixin + GenericAPIView
    # return self.create(request)